//
//  LZBoundPhoneViewController.m
//  DoctorFAQs
//
//  Created by langZai on 2017/2/17.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "LZBoundPhoneViewController.h"
#import "LZBoundPhoneNextViewController.h"

@interface LZBoundPhoneViewController ()
/** 电话号码textField */
@property (nonatomic, strong) UITextField *phoneTextField;
/** 验证码 */
@property (nonatomic, strong) UITextField *safeCodeTextField;
/** 获取验证码Btn */
@property (nonatomic, strong) UIButton *getBtn;
/** 下一步Btn */
@property (nonatomic, strong) UIButton *nextBtn;

@end

@implementation LZBoundPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KHColorFromRGB(0xffffff);
    self.navigationItem.title = @"绑定手机";
    [self createUI];
}

- (void)createUI{
    
    UILabel *numLabel = [[UILabel alloc] init];
    numLabel.text = @"手机号:+86";
    numLabel.font = FONT(15);
    numLabel.textColor = KHColorFromRGB(0x333333);
    numLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:numLabel];
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(45 *xsIZE);
        make.top.equalTo(self.view).offset(70 *ysIZE);
    }];
    
    // 手机号textField
    _phoneTextField = [[UITextField alloc] init];
    //_phoneTextField.delegate = self;
    _phoneTextField.font = NEWFONT(15);
    _phoneTextField.placeholder = @"您的手机号码";
    _phoneTextField.textColor = KHColorFromRGB(0xbfc3cf);
    _phoneTextField.textAlignment = NSTextAlignmentLeft;
    _phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    //_phoneTextView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_phoneTextField];
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake((SCREEN_Width - 200) * xsIZE, 30 *ysIZE));
        make.left.equalTo(self.view.mas_left).offset(140*xsIZE);
        make.centerY.equalTo(numLabel);
    }];
    
    // 横线
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = KHColorFromRGB(0x4bb92c);
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(45*xsIZE);
        make.right.equalTo(self.view).offset(-45*xsIZE);
        make.top.equalTo(self.view).offset(100 *ysIZE);
        make.height.offset(ysIZE);
    }];
    
    // 验证码
    UILabel *safeCodeLabel = [[UILabel alloc] init];
    safeCodeLabel.text = @"验证码";
    safeCodeLabel.font = FONT(15);
    safeCodeLabel.textColor = KHColorFromRGB(0x333333);
    safeCodeLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:safeCodeLabel];
    [safeCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(45 * xsIZE);
        make.top.equalTo(lineView.mas_bottom).offset(20 *ysIZE);
    }];
    
    // 验证码textField
    _safeCodeTextField = [[UITextField alloc] init];
    _safeCodeTextField.keyboardType = UIKeyboardTypeNumberPad;
    _safeCodeTextField.font = NEWFONT(15);
    _safeCodeTextField.textColor = KHColorFromRGB(0xbfc3cf);
    _safeCodeTextField.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:_safeCodeTextField];
    [_safeCodeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake((SCREEN_Width - 200) *xsIZE, 30 *ysIZE));
        make.left.equalTo(self.view.mas_left).offset(140*xsIZE);
        make.centerY.equalTo(safeCodeLabel);
    }];
    
    // 下面的横线
    UIView *lineViewB = [[UIView alloc] init];
    lineViewB.backgroundColor = KHColorFromRGB(0x4bb92c);
    [self.view addSubview:lineViewB];
    [lineViewB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(45*xsIZE);
        make.right.equalTo(self.view).offset(-45*xsIZE);
        make.height.offset(ysIZE);
        make.top.equalTo(lineView.mas_bottom).offset(50 *ysIZE);
        
    }];
    
    // 获取验证码button
    _getBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_getBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_getBtn setTitleColor:KHGreenColor forState:UIControlStateNormal];
    _getBtn.titleLabel.font = NEWFONT(14);
    [_getBtn setBackgroundColor:KHColorFromRGB(0xffffff)];
    [self.view addSubview:_getBtn];
    [_getBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(80*xsIZE, 25*ysIZE));
        make.right.equalTo(self.view.mas_right).offset(-45 *xsIZE);
        make.centerY.equalTo(_safeCodeTextField);
    }];
    [_getBtn addTarget:self action:@selector(getButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    // 下一步按钮
    _nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_nextBtn setTitle:@"绑定" forState:UIControlStateNormal];
    [_nextBtn setBackgroundColor:KHColorFromRGB(0x7bd661)];
    _nextBtn.titleLabel.font = NEWFONT(18);
    _nextBtn.layer.cornerRadius = 5;
    [_nextBtn setTitleColor:KHColorFromRGB(0xffffff) forState:UIControlStateNormal];
    [self.view addSubview:_nextBtn];
    [_nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(45*xsIZE);
        make.right.equalTo(self.view).offset(-45*ysIZE);
        make.top.equalTo(lineViewB.mas_bottom).offset(80 *ysIZE);
        make.height.offset(35*ysIZE);
    }];
    [_nextBtn addTarget:self action:@selector(nextButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
}


- (void)getButtonClick {
    if (!Predicate(PhoneRegex,_phoneTextField.text)) {
        [YJProgressHUD showMessage:@"请输入正确的手机号码" inView:self.view];
        return;
    }
    [self openCountdown];
}


// 点击下一步
- (void)nextButtonClick {
    NSLog(@"点击下一步");
    LZBoundPhoneNextViewController *boundPhoneNextVC = [[LZBoundPhoneNextViewController alloc] init];
    [self.navigationController pushViewController:boundPhoneNextVC animated:YES];
}

// 按钮倒计时方法
-(void)openCountdown{
    __block NSInteger time = 59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(time <= 0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮的样式
                [self.getBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                [self.getBtn setTitleColor:KHGreenColor forState:UIControlStateNormal];
                self.getBtn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = time % 60;
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置按钮显示读秒效果
                [self.getBtn setTitle:[NSString stringWithFormat:@"%.2d秒", seconds] forState:UIControlStateNormal];
                [self.getBtn setTitleColor:KHGreenColor forState:UIControlStateNormal];
                self.getBtn.userInteractionEnabled = NO;
            });
            time--;
        }
    });
    dispatch_resume(_timer);
}



@end
